﻿using ConfExpert.Utility;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Drawing;

namespace ConfExpert.Forms.Depo
{
    public partial class SyncKart : XPopupForm
    {
        public SyncKart(string s)
        {
            InitializeComponent();
            idxs = s;
        }
        string idxs;

        GridBand bandT, bandKl, bandKl2, bandKm, bandKn, bandF, bandE, bandA;
        private void SyncKart_Load(object sender, EventArgs e)
        {

            gv.Init("GetSyncKart2", idxs);
            gv.Images = bgvIcons;
            gv.RowCellClick += new RowCellClickEventHandler(gv_RowCellClick);
            gv.RowCellStyle += new RowCellStyleEventHandler(gv_RowCellStyle);
            gv.SetCaptions(
                "KstKaliteId:KST", "KaliteId:Kart",
                "KstKaliteAciklama:KST", "KaliteAciklama:Kart",
                "KstKompozisyonId:KST", "KompozisyonId:Kart",
                "KstKonstruksiyon:KST", "Konstruksiyon:Kart",
                "KstFinishId:KST", "FinishId:Kart",
                "KstEnGrubu:KST", "EnGrubu:Kart",
                "KstAgirlik:KST", "Agirlik:Kart"
                );

            bandT = gv.Bands.AddBand("ARTİKEL");
            bandKl = gv.Bands.AddBand("Kalite");
            bandKl2 = gv.Bands.AddBand("Kalite Açıklama");
            bandKm = gv.Bands.AddBand("Kompozisyon");
            bandKn = gv.Bands.AddBand("Konstruksiyon");
            bandF = gv.Bands.AddBand("Finish");
            bandE = gv.Bands.AddBand("En Grubu");
            bandA = gv.Bands.AddBand("Ağırlık");

            bandKl.ImageIndex = 1;
            bandKl2.ImageIndex = 4;
            bandKm.ImageIndex = 3;
            bandKn.ImageIndex = 5;
            bandF.ImageIndex = 2;
            bandE.ImageIndex = 4;
            bandA.ImageIndex = 3;

            foreach (string colName in "idx,Scan,PRO".Split(','))
                bandT.Columns.Add(gv.Columns[colName]);
            gv.MakeColumnInvisible("idx");

            foreach (string colName in "KstKaliteId,KaliteId".Split(','))
                bandKl.Columns.Add(gv.Columns[colName]);

            foreach (string colName in "KstKaliteAciklama,KaliteAciklama".Split(','))
                bandKl2.Columns.Add(gv.Columns[colName]);

            foreach (string colName in "KstKompozisyonId,KompozisyonId".Split(','))
                bandKm.Columns.Add(gv.Columns[colName]);

            foreach (string colName in "KstKonstruksiyon,Konstruksiyon".Split(','))
                bandKn.Columns.Add(gv.Columns[colName]);

            foreach (string colName in "KstFinishId,FinishId".Split(','))
                bandF.Columns.Add(gv.Columns[colName]);

            foreach (string colName in "KstEnGrubu,EnGrubu".Split(','))
                bandE.Columns.Add(gv.Columns[colName]);

            foreach (string colName in "KstAgirlik,Agirlik".Split(','))
                bandA.Columns.Add(gv.Columns[colName]);

            gv.Columns["KstKaliteId"].ColumnEdit = RI(Rp.Kalite);
            gv.Columns["KstKompozisyonId"].ColumnEdit = RI(Rp.Kompozisyon);
            gv.Columns["KstFinishId"].ColumnEdit = RI(Rp.Finish);
            gv.Columns["KstEnGrubu"].ColumnEdit = RI(Rp.EnGrubu);

            bandT.Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
            gv.BestFitColumns();


            btnKaydet.ItemClick += btnKaydet_ItemClick;

            sel[bandKl] = "KstKaliteId";
            sel[bandKl2] = "KstKaliteAciklama";
            sel[bandKm] = "KstKompozisyonId";
            sel[bandKn] = "KstKonstruksiyon";
            sel[bandF] = "KstFinishId";
            sel[bandE] = "KstEnGrubu";
            sel[bandA] = "KstAgirlik";

            FooterText = "Renkli sütünlar aktarılacak bilgileri göstermektedir.";
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();
            string xml = gv.Serialize();

            ExecuteNonQuery("UpdSyncKart2", xml,
                sel[bandKl].Contains("Kst"),
                sel[bandKl2].Contains("Kst"),
                sel[bandKm].Contains("Kst"),
                sel[bandKn].Contains("Kst"),
                sel[bandF].Contains("Kst"),
                sel[bandE].Contains("Kst"),
                sel[bandA].Contains("Kst")
                );
            AppSession.MainForm.RefreshData();

            ShowMessage("Bilgiler başarı ile aktarıldı.");
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        void gv_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                if (sel.ContainsValue(e.Column.FieldName)
                    && e.Column.FieldName.StartsWith("Kst"))
                    e.Appearance.BackColor = e.Appearance.BackColor2 = Color.SkyBlue;
                else
                    e.Appearance.Combine((e.RowHandle & 1) != 0 ? gv.Appearance.EvenRow : gv.Appearance.OddRow);
            }
        }
        Dictionary<GridBand, string> sel = new Dictionary<GridBand, string>();

        //void gv_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        //{
        //    if (e.RowHandle > -1)
        //    {
        //        if (sel.ContainsValue(e.Column.FieldName)
        //            && e.Column.FieldName.StartsWith("Kst"))
        //            e.Appearance.BackColor = e.Appearance.BackColor2 = Color.SkyBlue;
        //        else
        //            e.Appearance.Combine((e.RowHandle & 1) != 0 ? gv.Appearance.EvenRow : gv.Appearance.OddRow);

        //        if (e.Column.FieldName.StartsWith("Kst"))
        //            e.Graphics.DrawLine(new Pen(Color.Black, 2), e.Bounds.Left - 1, e.Bounds.Y - 2, e.Bounds.Left - 1, e.Bounds.Bottom + 2);
        //    }

        //}


        void gv_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle > -1)
                sel[((BandedGridColumn)gv.FocusedColumn).OwnerBand] = e.Column.FieldName;

            gv.LayoutChanged();
        }
    }
}